# coding=utf-8
import requests, uuid, hashlib, json
from application import app, cache
from common.libs.DateTimeLibs import get_now_time_str, str_to_timestamp

from common.libs.order.YLYLib.Config.config import Config
from common.libs.order.YLYLib.Oauth.oauth import Oauth
from common.libs.order.YLYLib.Protocol.rpc_client import RpcClient
from common.libs.order.YLYLib.Api.yly_print import YlyPrint

"""
易联云打印机
"""

class YiLianYunLibs () :
    def __init__(self):
        self.customer_id = app.config["YILIANYUN"]["customer_id"]
        self.app_id = app.config["YILIANYUN"]["app_id"]
        self.secret = app.config["YILIANYUN"]["secret"]
        self.machine_code = app.config["YILIANYUN"]["machine_code"]
        self.config = Config( self.app_id, self.secret)
        self.oauth_client = Oauth(self.config)
        self.access_token = self.__get_access_token()
        self.rpc_client = RpcClient(self.config, self.access_token)

    def __get_access_token(self):
        a_cache_name = "yilianyun:access_token"
        access_token = cache.get(a_cache_name)
        if access_token :
            return access_token
        token_data = self.oauth_client.get_token()
        app.logger.error(token_data)
        access_token = token_data['body']['access_token']
        cache.set(a_cache_name, access_token, 86400)
        return access_token

    def get_client(self):
        return YlyPrint(self.rpc_client)


    # def get_access_token(self):
    #     a_cache_name = "yilianyun:access_token"
    #     r_cache_name = "yilianyun:refresh_token"
    #     access_token = cache.get(a_cache_name)
    #     if access_token :
    #         return access_token
    #
    #     url = "https://open-api.10ss.net/oauth/oauth"
    #     headers = {'Content-Type': 'application/x-www-form-urlencoded'}
    #     timestamp = str_to_timestamp(get_now_time_str())
    #     data = {
    #         "client_id" : self.app_id,
    #         "grant_type" : "client_credentials",
    #         "sign" : self.get_sign(timestamp),
    #         "scope" : "all",
    #         "timestamp" : timestamp,
    #         "id" : self.get_uuid()
    #     }
    #     r = requests.post(url=url, data=data, headers=headers)
    #     r.encoding = "utf-8"
    #     res = json.loads(r.text)
    #     if int(res["error"]) == 0 :
    #         access_token = res["body"]["access_token"]
    #         refresh_token = res["body"]["refresh_token"]
    #         cache.set(a_cache_name, access_token, 86400)
    #         cache.set(r_cache_name, refresh_token, 86400)
    #         return access_token
    #     else :
    #         return False

    # def get_sign(self, timestamp):
    #     access_token = str(self.app_id) + str(timestamp) + str(self.secret)
    #     m = hashlib.md5()
    #     m.update(access_token.encode("utf-8"))  # 密码MD5加密
    #     return str(m.hexdigest()).lower()
    #
    # def get_uuid(self):
    #     return str(uuid.uuid4()).upper()
    #
    # def print_text(self, content, order_sn):
    #     url = "https://open-api.10ss.net/print/index"
    #     timestamp = str_to_timestamp(get_now_time_str())
    #     data = {
    #         "client_id" : self.app_id,
    #         "access_token": self.get_access_token(),
    #         "machine_code": self.machine_code,
    #         "content" : content,
    #         "origin_id" : order_sn,
    #         "sign" : self.get_sign(timestamp),
    #         "id" : self.get_uuid(),
    #         "timestamp" : timestamp
    #     }
    #     headers = {'Content-Type': 'application/x-www-form-urlencoded'}
    #     r = requests.post(url=url, data=data, headers=headers)
    #     r.encoding = "utf-8"
    #     res = json.loads(r.text)
    #     if int(res["error"]) == 0 :
    #         return True
    #     else :
    #         return False
    #
